package vn.com.shoppie.activity2;

import vn.com.shoppie.activity.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

@SuppressLint("ViewConstructor")
public class LayoutTab extends LinearLayout implements OnTouchListener, OnClickListener {
	public static boolean SHOW_INDICATOR = true;

	public int id = -1;
	private boolean state = false;
	private TabIndicator indicator;

	public LayoutTab(Context context, int id, String title, int idImage, boolean state) {
		super(context);
		LayoutInflater.from(context).inflate(R.layout.layout_tab, this, true);

		TextView tv = (TextView) findViewById(R.id.textView1);
		ImageView img = (ImageView) findViewById(R.id.imageView1);

		
		

		this.id = id;
		this.state = state;

		// create Indicator
		indicator = new TabIndicator(context, id, state);
		
		if(title==null || title.equals("")){
			tv.setVisibility(View.GONE);
		}else{
			tv.setVisibility(View.VISIBLE);
			tv.setText(title);
		}
		if(idImage<0){
			img.setVisibility(View.GONE);
		}else{
			img.setVisibility(View.VISIBLE);
			img.setImageResource(idImage);
		}
	}

	public TabIndicator getIndicator() {
		return indicator;
	}

	public void setState(boolean isChoosed) {
		state = isChoosed;
		this.getIndicator().setState(state);
	}

	public int getId() {
		return id;
	}

	public boolean getState() {
		return state;
	}

	public class TabIndicator extends LinearLayout {
		private int id = -1;
		private boolean state = false;

		public TabIndicator(Context context, int id, boolean isChoosed) {
			super(context);
			LayoutInflater.from(context).inflate(R.layout.layout_indicator, this, true);
			state = isChoosed;
		}

		public void setState(boolean isChoosed) {
			state = isChoosed;
			int stateView = View.VISIBLE;
			if (!state) {
				stateView = View.INVISIBLE;
			}
			if (!SHOW_INDICATOR) {
				stateView = View.GONE;
			}
			this.setVisibility(stateView);
			if (_listener != null) {
				_listener.onStateChange(stateView);
			}
		}

		public int getId() {
			return id;
		}

		public boolean getState() {
			return state;
		}
	}

	@Override
	public void onClick(View arg0) {
		if (_listener != null) {
			_listener.onClick(arg0);
		}
	}

	@Override
	public boolean onTouch(View arg0, MotionEvent arg1) {
		if (_listener != null) {
			return _listener.onTouch(arg0, arg1);
		}
		return false;
	}

	private OnItemTabSelectListener _listener;
	public void setOnItemTabSelectListener(OnItemTabSelectListener listener){
		this._listener=listener;
	}

	public interface OnItemTabSelectListener {
		public void onClick(View v);

		public boolean onTouch(View v, MotionEvent event);

		public void onStateChange(int state);
	}
}
